package red.cola.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import red.cola.auth.service.SysRoleService;
import red.cola.model.system.SysRole;
import red.cola.result.Result;
import red.cola.vo.system.SysRoleQueryVo;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Name: SysRoleController
 * @Author: Cola
 * @Time: 2023/3/6 11:25
 * @Description: SysRoleController
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Resource
    private SysRoleService roleService;

    /**
     * 获取全部角色列表
     *
     * @return Result<List < SysRole>>
     */
    @ApiOperation("获取全部角色列表")
    @GetMapping("findAll")
    public Result<List<SysRole>> findAllSysRole() {
        List<SysRole> roleList = roleService.list();
        return Result.ok(roleList);
    }

    /**
     * 条件分页查询
     *
     * @return Result
     */

    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQuerySysRole(@PathVariable Long page,
                                   @PathVariable Long limit,
                                   SysRoleQueryVo sysRoleQueryVo) {

        /*
        创建 PageInfo 对象
        */
        Page<SysRole> pageInfo = new Page<>(page, limit);

        /*
        封装查询条件
        */
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)) {
            /*条件存在，进行封装*/
            wrapper.like(SysRole::getRoleName, roleName);
        }

        IPage<SysRole> pageModel = roleService.page(pageInfo, wrapper);
        return Result.ok(pageModel);
    }

    /**
     * 根据 ID 获取角色
     *
     * @param id ID
     * @return SysRole
     */

    @ApiOperation(value = "根据 ID 获取角色")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        SysRole role = roleService.getById(id);
        return Result.ok(role);
    }

    /**
     * 新增角色
     *
     * @param role
     * @return Result.ok
     */

    @ApiOperation(value = "新增角色")
    @PostMapping("save")
    public Result save(@RequestBody SysRole role) {
        roleService.save(role);
        return Result.ok();
    }

    /**
     * 修改角色
     *
     * @param role
     * @return Result.ok
     */

    @ApiOperation(value = "修改角色")
    @PutMapping("update")
    public Result updateById(@RequestBody SysRole role) {
        roleService.updateById(role);
        return Result.ok();
    }

    /**
     * 根据 ID 删除角色
     *
     * @param id ID
     * @return Result.ok
     */

    @ApiOperation(value = "根据 ID 删除角色")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        roleService.removeById(id);
        return Result.ok();
    }

    /**
     * 根据 IDS 列表删除角色
     *
     * @param idList IDS 列表
     * @return Result.ok
     */

    @ApiOperation(value = "根据 IDS 列表删除角色")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        roleService.removeByIds(idList);
        return Result.ok();
    }

}
